package com.tianhui.consignortwofei.pattern.orderStatus.status.impl;

import android.text.TextUtils;

import com.fgs.common.api.CommonConfig;
import com.fgs.common.utils.CheckUtil;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.constant.ImageType;
import com.tianhui.consignortwofei.constant.OrderStatus;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.mvp.model.enty.DisplayPictureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderCondition;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailItemInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailLineInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailPictureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailRemarkInfo;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.CheckDriverOperate;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @author fgs
 * @date 2020/8/27
 * @description 调度单
 **/
public class DaFuDispatchStatus implements IOrderStatus {
    @Override
    public Observable getOrderListObservable(OrderCondition orderCondition) {
        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("offset", "1");
        map.put("order", "desc");
        map.put("isstatus", "9");
        map.put("sort", "robbilltime");
        map.put("page", orderCondition.page + "");
        if (!TextUtils.isEmpty(orderCondition.searchKey)) {
            String searchKey = orderCondition.searchKey;
            //完整的调度单号去掉首字母，剩余全为数字
            if (orderCondition.searchKey.contains("D") && orderCondition.searchKey.length() > 6) {
                searchKey = orderCondition.searchKey.substring(1);
            }
            if (!TextUtils.isEmpty(searchKey) && CheckUtil.isNumCharacters(searchKey)) { //
//                map.put("dispatchno", orderCondition.searchKey);
                map.put("businessno", orderCondition.searchKey);
            } else if (CheckUtil.isChineseCharacters(orderCondition.searchKey)) {
                map.put("drivername", orderCondition.searchKey);
            } else {
                map.put("vehiclenum", orderCondition.searchKey);
            }
        }
        return CommonConfig.getInstance().create(OrderApiService.class).dispatchList(map);
    }

    @Override
    public Observable getOrderDetailObservable(OrderCondition orderCondition) {
        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("offset", "1");
        map.put("order", "desc");
        map.put("sort", "create_time");
        map.put("page", orderCondition.page + "");
        map.put("dispatchno", orderCondition.dispatchNo);
        return CommonConfig.getInstance().create(OrderApiService.class).dispatchList(map);
    }

    @Override
    public List getOrderDetailList(OrderInfo orderInfo) {
        List list = new ArrayList<>();

        DetailInfo detailInfo;
        DetailItemInfo detailItemInfo;
        DetailRemarkInfo remarkInfo;
        DetailPictureInfo pictureInfo;
        DetailLineInfo lineInfo;

        detailInfo = new DetailInfo();

        detailInfo.topLeftText = "货源单号：" + orderInfo.billno;
        detailInfo.topRightText = "发单人：" + orderInfo.placeusername;

        detailInfo.infoList = new ArrayList<>();

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交单价：";
        detailItemInfo.content = orderInfo.baseprice + "（元/吨）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交金额：";
        detailItemInfo.content = orderInfo.tradtotalprice + "（元）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交单价（含税）：";
        detailItemInfo.content = orderInfo.tradtaxprice + "（元/吨）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交金额（含税）：";
        detailItemInfo.content = orderInfo.totaltradtaxprice + "（元）";
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "委托人：";
        detailItemInfo.content = orderInfo.clientperson;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "委托人电话：";
        detailItemInfo.content = orderInfo.clienttel;
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "业务员：";
        detailItemInfo.content = orderInfo.destinationusername;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "业务员电话：";
        detailItemInfo.content = orderInfo.destinationtel;
        detailInfo.infoList.add(detailItemInfo);


        detailInfo.bottomLeftText = "货物类型：" + orderInfo.goodstypetext;
        String transportType = "不含税运输";
        if (!TextUtils.isEmpty(orderInfo.isdelegateshipment) && orderInfo.isdelegateshipment.equals("1")) {
            transportType = "含税运输";
        }
        detailInfo.bottomRightText = transportType;

        list.add(detailInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        pictureInfo = new DetailPictureInfo();
        pictureInfo.list = new ArrayList<>();
        DisplayPictureInfo displayPictureInfo;
        displayPictureInfo = new DisplayPictureInfo();
        displayPictureInfo.title = "装车回单";
        displayPictureInfo.type = ImageType.TYPE_LOADING_BILL;
        displayPictureInfo.imageId = orderInfo.poundimg;
        displayPictureInfo.orderInfo = orderInfo;
//        displayPictureInfo.imageId = "8238,8239,8240,8241";
        pictureInfo.list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
        displayPictureInfo.title = "卸车回单";
        displayPictureInfo.type = ImageType.TYPE_DELIVER_BILL;
        displayPictureInfo.imageId = orderInfo.recepitbill;
        displayPictureInfo.orderInfo = orderInfo;
        pictureInfo.list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
        displayPictureInfo.title = "装车车辆";
        displayPictureInfo.type = ImageType.TYPE_LOADING_VEHICLE;
        displayPictureInfo.imageId = orderInfo.loadingimg;
        displayPictureInfo.orderInfo = orderInfo;
        pictureInfo.list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
        displayPictureInfo.title = "卸车车辆";
        displayPictureInfo.type = ImageType.TYPE_DELIVER_VEHICLE;
        displayPictureInfo.imageId = orderInfo.unloadimg;
        displayPictureInfo.orderInfo = orderInfo;
        pictureInfo.list.add(displayPictureInfo);
        list.add(pictureInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.title = "重量信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.spanCount = 2;
        detailInfo.infoList = new ArrayList<>();

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交重量：";
        detailItemInfo.content = orderInfo.tradweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "装车重量：";
        detailItemInfo.content = orderInfo.tranweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "";
        detailItemInfo.content = "";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "确认装车重量：";
        detailItemInfo.content = orderInfo.tranweightconfirm + "吨";
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "亏吨重量：";
        detailItemInfo.content = orderInfo.loseweight + "吨";
//        detailItemInfo.content = CalculationController.getInstance().getLoseWeight(orderInfo) + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "收货重量：";
        detailItemInfo.content = orderInfo.recelveweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "";
        detailItemInfo.content = "";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "确认收货重量：";
        detailItemInfo.content = orderInfo.recelveweightconfirm + "吨";
        detailInfo.infoList.add(detailItemInfo);

        list.add(detailInfo);

        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.operateText = "查看";
        detailInfo.iOrderOperate = new CheckDriverOperate();
        detailInfo.title = "司机信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();
        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "姓名：";
        detailItemInfo.content = orderInfo.drivername;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "车牌号：";
        detailItemInfo.content = orderInfo.vehiclenum;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "联系方式：";
        detailItemInfo.content = orderInfo.drivertel;
        detailInfo.infoList.add(detailItemInfo);

        list.add(detailInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.title = "司磅信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "司磅地点：";
        detailItemInfo.content = orderInfo.poundpalce;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "司磅时间：";
        detailItemInfo.content = orderInfo.poundtime;
        detailInfo.infoList.add(detailItemInfo);

        list.add(detailInfo);

        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.title = "时间信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "抢单时间：";
        detailItemInfo.content = orderInfo.robbilltime;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "创建时间：";
        detailItemInfo.content = orderInfo.create_time;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "发单时间：";
        detailItemInfo.content = orderInfo.sendordertime;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "装车时间：";
        detailItemInfo.content = orderInfo.trantime;
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "收货时间：";
//        detailItemInfo.content = orderInfo.recepittime;
        detailItemInfo.content = orderInfo.recelvetime;
        detailInfo.infoList.add(detailItemInfo);
        list.add(detailInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "预计卸车时间：";
//        detailItemInfo.content = orderInfo.recepittime;
        detailItemInfo.content = orderInfo.unloadearlhours + " 小时";
        detailInfo.infoList.add(detailItemInfo);
        list.add(detailInfo);


        remarkInfo = new DetailRemarkInfo();
        String topLeft = "付款状态：<font color='#f52225'>" + "未付款" + "</font>";
        if (!TextUtils.isEmpty(orderInfo.issettle)) {
            if (orderInfo.issettle.equals("1")) {
                topLeft = "付款状态：<font color='#669900'>" + "付款中" + "</font>";
            } else if (orderInfo.issettle.equals("2")) {
                topLeft = "付款状态：<font color='#00ff00'>" + "已付款" + "</font>";
            }
        }

        String topRight = "计算状态：<font color='#f52225'>" + "未计算" + "</font>";
        if (!TextUtils.isEmpty(orderInfo.iscount)) {
            if (orderInfo.iscount.equals("1")) {
                topRight = "计算状态：<font color='#00ff00'>" + "已计算" + "</font>";
            }
        }
        remarkInfo.topLeft = topLeft;
        remarkInfo.topRight = topRight;
        remarkInfo.title = "货主备注";
        remarkInfo.isShowTitleTip = true;
        StringBuilder remark = new StringBuilder();
        if (!TextUtils.isEmpty(orderInfo.transremark)) {
            remark.append(orderInfo.transremark);
        }
        if (!TextUtils.isEmpty(orderInfo.recelveremark)) {
            remark.append("\n");
            remark.append(orderInfo.recelveremark);
        }
        if (!TextUtils.isEmpty(orderInfo.endorderremark)) {
            remark.append("\n");
            remark.append(orderInfo.endorderremark);
        }
        remarkInfo.remark = remark.toString();

        list.add(remarkInfo);


        return list;
    }


    @Override
    public int getOrderStatusType() {
        return OrderType.TYPE_DISPATCH;
    }

    @Override
    public String getOrderStatus(OrderInfo orderInfo) {
        String status = "";
        if (orderInfo.isstate == OrderStatus.CALCULATED) {
            status = "状态：<font color='#8345b7'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.TO_BE_LOADED) {
            status = "状态：<font color='#f52225'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.TO_BE_COLLECTED) {
            status = "状态：<font color='#26bb5b'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.CANCELED) {
            status = "状态：<font color='#ee0a24'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.CANCELING) {
            status = "状态：<font color='#f95f62'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.IN_TRANSIT) {
            status = "状态：<font color='#ffa313'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.PAYMENT) {
            status = "状态：<font color='#669900'>" + orderInfo.isstatetext + "</font>";
        } else if (orderInfo.isstate == OrderStatus.COMPLETE) {
            status = "状态：<font color='#00ff00'>" + orderInfo.isstatetext + "</font>";
        } else {
            status = "状态：<font color='#A2A2A5'>" + orderInfo.isstatetext + "</font>";
        }
        return status;
    }

    @Override
    public String getDispatchNumber(OrderInfo orderInfo) {
        return orderInfo.dispatchno;
    }

    @Override
    public String getFreight(OrderInfo orderInfo) {

        return null;
    }

    @Override
    public String getSearchHint() {
        return "按销售单号或司机姓名或车牌号查询";
    }
    public int getFeiOrderType() {
        return OrderType.TYPE_PAYING_TIME;
    }
}
